package com.sophialex.health.record.ui.account;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.CloseWindowListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.part.ViewPart;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.record.Activator;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.Constants;
import com.sophialex.health.record.common.SOAPServiceDispath;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.config.style.EnumType.EScreenType;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.ViewUitl;



public abstract class PersonAccountUI extends ViewPart {

	public static final String ID = "com.sophialex.health.record.ui.account.PersonAccountUI"; //$NON-NLS-1$
	private ToolBar toolBar;
	protected ToolItem printItem,returnItem;
	
	protected Composite compRightTop;
	protected Composite compRightBottom;

	protected Image img_head = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/head.bmp"); //$NON-NLS-1$
	protected Image img_hadeBj = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/headbj2.bmp"); //$NON-NLS-1$
	
	protected Color col_bj1 = RcpResourceManager.getColor(230, 232, 238);
	protected Color col_bj2 = RcpResourceManager.getColor(244, 245, 246);
	protected Color col_stite = RcpResourceManager.getColor(0, 0, 255);
	protected Color col_stite2 = RcpResourceManager.getColor(0, 0, 0);
	protected Image img_left1 = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/left1.png"); //$NON-NLS-1$
	protected Image img_right1 = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/right1.png"); //$NON-NLS-1$

	protected Image img_left2 = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/left2.png"); //$NON-NLS-1$
	protected Image img_right2 = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/right2.png"); //$NON-NLS-1$
	private Action szAct;

	protected Composite compositeLeft;

	private Image img_bb_right = RcpResourceManager.getPluginImage(Activator
			.getDefault(), "icons/bb_right.bmp"); //$NON-NLS-1$

	private Label labSwitch;

	private SashForm sashForm;

	private Composite compositeRight;
	
	protected Browser browser;

	public PersonAccountUI() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setBackground(col_bj2);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			sashForm = new SashForm(container, SWT.NONE);
			// sashForm.setBackgroundImage(img_bj);
			sashForm.setBackground(col_bj2);
			{
				compositeLeft = new Composite(sashForm, SWT.BORDER);
				compositeLeft.setLayout(new FormLayout());
				compositeLeft.setBackground(col_bj2);
			}
			{
				compositeRight = new Composite(sashForm, SWT.NONE);
				compositeRight.setBackground(col_bj2);
				compositeRight.setLayout(new FormLayout());
				{
					labSwitch = new Label(compositeRight, SWT.NONE);
					labSwitch.setImage(img_left1);
					{
						FormData formData = new FormData();
						formData.top = new FormAttachment(50);
						formData.left = new FormAttachment(0);
						labSwitch.setLayoutData(formData);
					}
					labSwitch.setToolTipText(""); //$NON-NLS-1$
				}
				
				
				{
					toolBar = new ToolBar(compositeRight, SWT.FLAT);
					toolBar.setBackgroundImage(ImageUtil.toolbar);
					{
						FormData formData = new FormData();
						formData.height = 50;
						formData.right = new FormAttachment(100);
						formData.top = new FormAttachment(0);
						formData.left = new FormAttachment(0);
						toolBar.setLayoutData(formData);
					}
					{
						printItem = new ToolItem(toolBar, SWT.NONE);
						printItem.setImage(ImageUtil.print);
						printItem.setText(Messages.getString("PersonAccount.2")); //$NON-NLS-1$
						printItem.addSelectionListener(new SelectionAdapter() {
							public void widgetSelected(SelectionEvent e) {
								doPrint();
							}
						});
					}
					{
						returnItem = new ToolItem(toolBar, SWT.NONE);
						returnItem.setImage(ImageUtil.import_zc);
						returnItem.setText(Messages.getString("PersonAccount.3")); //$NON-NLS-1$
						returnItem.addSelectionListener(new SelectionAdapter() {
							public void widgetSelected(SelectionEvent e) {
								doReturn();
							}
						});
						
						Composite composite = new Composite(compositeRight, SWT.NONE);
						FormData fd_composite = new FormData();
						fd_composite.top = new FormAttachment(toolBar, 0);
						fd_composite.left = new FormAttachment(labSwitch, 0, SWT.LEFT);
						fd_composite.bottom = new FormAttachment(100,0);
						fd_composite.right = new FormAttachment(100,0);
						composite.setLayoutData(fd_composite);
                        
						composite.setLayout(new FillLayout(SWT.HORIZONTAL));
						browser = new Browser(composite, SWT.NONE);
						String url = SOAPServiceDispath.serviceMapAccount.get(Constants.GRJKDA_BUSSTYPE) + SuperContext.currentPerson.getId();
						browser.setUrl(url);
						
					}
					
				}
				
				
				
				

				
			}
			int leftWridth = 220;
			int rightWridth = EScreenType.getScreenSize().x - 300;
			sashForm.setWeights(new int[] { 200, rightWridth });
		}
		initializeToolBar();
		initializeMenu();
		buidServiceControl(compositeLeft);
		addlistener();
		
		this.setPartName(Messages.getString("PersonAccount.0"));
	}

	/** 返回 */
	protected abstract void doReturn();
    
	protected abstract void doPrint();

	
	private void addlistener() {

		labSwitch.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (compositeLeft.getVisible()) {
					compositeLeft.setVisible(false);
					sashForm.setMaximizedControl(compositeRight);
					labSwitch.setImage(img_right1);
					labSwitch.setToolTipText(""); //$NON-NLS-1$
					sashForm.layout();
				} else {
					compositeLeft.setVisible(true);
					sashForm.setMaximizedControl(null);
					labSwitch.setImage(img_left1);
					labSwitch.setToolTipText(""); //$NON-NLS-1$
					sashForm.layout();
				}

			}
		});
		labSwitch.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseExit(MouseEvent e) {
				labSwitch.setImage(compositeLeft.getVisible() ? img_left1
						: img_right1);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				labSwitch.setImage(compositeLeft.getVisible() ? img_left2
						: img_right2);
			}
		});
		

	}
	
	protected void disposeCopmos(Composite com) {
		for (Control c : com.getChildren()) {
			if (!c.isDisposed()) {
				c.dispose();
			}
		}
	}

	/** 构建消息控件 */
	protected abstract void buidServiceControl(Composite com);



		
	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	private void setBackgroundImage(Composite com, Image img) {
		com.setBackgroundImage(img);
		for (Control c : com.getChildren()) {
			c.setBackgroundImage(img);
		}
	}

	private void setBackgroundColor(Composite com, Color color) {
		com.setBackground(color);
		for (Control c : com.getChildren()) {
			c.setBackground(color);
		}
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
}
